import {messages} from "share/common";
import React,{ Component } from 'react'
import { connect } from 'react-redux'
import { Button } from 'antd'
import SearchArea from 'components/search-area.js';
import SetSearchAreaForm from '../set-search-area-form';
import SetTableColumns from '../set-table-columns'
import CheckUtil from "../../../financial-management/check-center/check-util";
import checkCenterService from "../../../financial-management/check-center/check-center.service";
import menuRoute from "routes/menuRoute";
import OrderCenterServer from "../order-center-server";
import common from 'containers/financial-management/check-center/check-common';
import Table from 'components/table';

class ListPlane extends Component{

  constructor(props){
    super(props);
    this.searchParams = {// 获取表格数据默认是参数
      channel: "hly-admin",
      searchType: 'hlytime',
      companyOID: props.company.companyOID,
      dateFrom:common.getNextMonth('',-3),
      dateTo:common.getNextMonth('',0),
      page: 1,
      size: 10
    }

    this.state = {
      data:[],
      loading: false,
      consumerPlaneDetail:menuRoute.getRouteItem('consumer-plane-detail','key'),
      columns:SetTableColumns.getPlane(),
      searchFormData:SetSearchAreaForm.getPlaneForm(),// 搜所配置项
      pagination:{
        current:1,
        pageSize:10,
        total:0,
      },
    }
  }

  componentWillMount(){
    // 获取供应商
    checkCenterService.getSuppliers().then(res =>{
      const {searchFormData} = this.state;
      const options = [];
      res.data['flight'].length>0 && res.data['flight'].map(item =>{
        options.push({
          label:item.name,
          value:item.enName
        })
      });
      searchFormData[0].options = options;
      this.setState({searchFormData});
    })

    this.getPlaneList();

  }

  // 获取机票列表数据
  getPlaneList = () => {
    const { pagination } = this.state;
    OrderCenterServer(this).getPlaneData(this.searchParams).then(res => {
      res.data.airOrderInfoEntityVOs = this.addIndex(res.data.airOrderInfoEntityVOs);
      pagination.total = res.data.total;
      this.setState({
        data: res.data.airOrderInfoEntityVOs,
        pagination,
      })
    })
  }

  addIndex = (arr) => {
    const { pagination } = this.state;
    if(Array.isArray(arr)){
      arr = arr.map((item, index) => {
        item.index = (pagination.current - 1) * pagination.pageSize + (index + 1);
        return item;
      });
    }else{
      arr = [];
    }
    return arr;
  }

  /**
   * 表格变化(翻页操作)
   * @param pagination 分页数据对象(数据总数total，当前页码current，单页数size)
   */
  onTableChange = (pagination) => {
    console.log(pagination);
    this.searchParams.page = pagination.current;
    this.searchParams.size = pagination.pageSize;
    this.setState({pagination},()=>{
      this.getPlaneList();
    });
  }

  // 搜索
  onSearch = (values) => {
    this.searchParams = Object.assign({}, this.searchParams, SetSearchAreaForm.setPlaneFormParams(values));
    this.getPlaneList();
  }

  /**
   * 搜所区自定义事件
   * @param value 被监听组件返回的值
   * @param event 被监听对象触发的事件名称
   */
  onEvent = (value,event)=>{
    if(value === 'CHANGETIME_ORDERCENTER'){
      CheckUtil.handleEventDictionary(value, event, this.formRef)
    }
  }

  // 清空
  onClear = () => {
    console.log('clear');
    this.searchParams = {
      channel: "hly-admin",
      companyOID: this.props.company.companyOID,
      dateFrom:common.getNextMonth('',-3),
      dateTo:common.getNextMonth('',0),
      searchType: "hlytime",
      page: 1,
      size: 10
    }
    this.getPlaneList();
  }

  /**
   * 跳转详情
   * @param record 表格被点击行数据
   */
  onJumpDetail = (record) => {
    this.context.router.push(this.state.consumerPlaneDetail.url.replace(':recordId',record.orderId));
  }

  // 导出表格
  downloadExcel = () => {
    OrderCenterServer(this).exportOrderCenterData(this.searchParams).then(res => {
      console.log(res.data);
      res.data.exportFileRecord && window.open(res.data.exportFileRecord.tempUrl);
    })
  }

  render(){
    const { data, pagination, columns, searchFormData, loading } = this.state;
    return (
      <div>
        <SearchArea
          searchForm={searchFormData}
          submitHandle={this.onSearch}
          eventHandle={this.onEvent}
          clearHandle={this.onClear}
          maxLength={10}
          wrappedComponentRef={(inst) => this.formRef = inst}
        />
        <p style={{marginTop:'1em'}}>
          <Button onClick={this.downloadExcel} type='primary'>{messages('supplier.key564')/*Excel下载*/}</Button>
        </p>
        <Table
          rowKey='orderId'
          dataSource={data}
          pagination={pagination}
          columns={columns}
          loading={loading}
          onRow={record => ({
            onClick: () => this.onJumpDetail(record)
          })}
          bordered
          scroll={{ x: true, y: false }}
          onChange={this.onTableChange}
        />
      </div>
    )
  }
}

ListPlane.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
  }
}

export default connect(mapStateToProps)(ListPlane);
